﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SmartParking.Models;
using SmartParking.Bussiness;
using SmartParking.Data.Entities;
using SmartParking.Web.Models;

namespace SmartParking.Web.Controllers
{
    public class ManagerController : Controller
    {
        //
        // GET: /Index/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Manager/

        public ActionResult Manager()
        {
            return View("Manager", new CustomersModel());
        }

        //
        // GET: /Parkings/
        public ActionResult Parkings(int id = 0)
        {
            ParkingsModel model = new ParkingsModel(id);

            return View("Parkings", model);
        }

        //
        // GET: /Customer/
        public ActionResult Customer()
        {
            CustomerModel model = new CustomerModel();

            return View("Customer", model);
        }


        [HttpPost]
        public ActionResult SaveCustomer(CustomerModel model)
        {
            if (ModelState.IsValid)
            {
                ParkingManager.SaveCustomer((model.CustomerID == 0 ? string.Empty : model.CustomerID.ToString().Trim()), model.Name.Trim(), model.Email.Trim(), model.Phone.Trim(), model.Address.Trim(), 1, Request.UserHostAddress);

                return View("Manager", new CustomersModel());
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult UpdateCustomer(ParkingsModel model)
        {
            if (ModelState.IsValid)
            {
                ParkingManager.SaveCustomer((model.Customer.CustomerID == 0 ? string.Empty : model.Customer.CustomerID.ToString().Trim()), model.Customer.Name.Trim(), model.Customer.Email.Trim(), model.Customer.Phone.Trim(), model.Customer.Address.Trim(), 1, Request.UserHostAddress);

                model.Parkings = ParkingModel.LoadParkings(model.Customer.CustomerID);

                return View("Parkings", model);
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult DeleteCustomer(CustomerModel model)
        {
            ParkingManager.DeleteCustomer(model.CustomerID, 1, string.Empty);

            return View("Manager", new CustomersModel());
        }

        //
        // GET: /Parking/
        public ActionResult Parking(int id)
        {
            ParkingModel model = new ParkingModel();

            model.CustomerID = id;

            return View("Parking", model);
        }

        [HttpPost]
        public ActionResult SaveParking(ParkingModel model)
        {
            if (ModelState.IsValid)
            {
                ParkingManager.SaveParking(model.CustomerID, (model.ParkingID == 0 ? string.Empty : model.ParkingID.ToString().Trim()), model.Name.Trim(), model.LegalId.Trim(), model.LegalName.Trim(), model.BankAccount.Trim(), model.Address.Trim(), (!String.IsNullOrEmpty(model.PhoneNumber) ? model.PhoneNumber.Trim() : String.Empty), 1, Request.UserHostAddress);

                ParkingsModel parkings = new ParkingsModel(model.CustomerID);

                return View("Parkings", parkings);
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult UpdateParking(ParkZonesModel model)
        {
            if (ModelState.IsValid)
            {
                ParkingManager.SaveParking(model.Parking.CustomerID, (model.Parking.ParkingID == 0 ? string.Empty : model.Parking.ParkingID.ToString().Trim()), model.Parking.Name.Trim(), model.Parking.LegalId.Trim(), model.Parking.LegalName.Trim(), model.Parking.BankAccount.Trim(), model.Parking.Address.Trim(), (!String.IsNullOrEmpty(model.Parking.PhoneNumber) ? model.Parking.PhoneNumber.Trim() : String.Empty), 1, Request.UserHostAddress);

                model.ParkZones = ParkZoneModel.LoadParkZones(model.Parking.ParkingID);

                return View("ParkZones", model);
            }

            return View(model);
        }

        //
        // GET: /ParkZones/
        public ActionResult ParkZones(int id)
        {
            ParkZonesModel model = new ParkZonesModel(id);

            return View("ParkZones", model);
        }

        //
        // GET: /ParkZone/
        public ActionResult ParkZone(int id)
        {
            ParkZoneModel model = new ParkZoneModel();

            model.ParkingID = id;

            return View("ParkZone", model);
        }

        [HttpPost]
        public ActionResult SaveParkZone(ParkZoneModel model)
        {
            if (ModelState.IsValid)
            {
                ParkingManager.SaveParkingZone(model.ParkingID, (model.ParkZoneID == 0 ? string.Empty : model.ParkZoneID.ToString().Trim()), model.Name.Trim(), model.ManagerCommisionAmount, model.WebCommisionAmount, model.AllowHalfHour, (!String.IsNullOrEmpty(model.OpenHour) ? model.OpenHour.Trim() : String.Empty), (!String.IsNullOrEmpty(model.CloseHour) ? model.CloseHour.Trim() : String.Empty), model.RegularSpaces, model.SpacesToReserve, 1, Request.UserHostAddress);

                ParkZonesModel zones = new ParkZonesModel(model.ParkingID);

                return View("ParkZone", zones);
            }

            return View(model);
        }

        //[HttpPost]
        //public ActionResult UpdateParkZone(ParkZonesModel model)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        ParkingManager.SaveParkingZone(model.Parking.ParkingID, (model.ParkZoneID == 0 ? string.Empty : model.ParkZoneID.ToString().Trim()), model.Name.Trim(), model.ManagerCommisionAmount, model.WebCommisionAmount, model.AllowHalfHour, (!String.IsNullOrEmpty(model.OpenHour) ? model.OpenHour.Trim() : String.Empty), (!String.IsNullOrEmpty(model.CloseHour) ? model.CloseHour.Trim() : String.Empty), model.RegularSpaces, model.SpacesToReserve, 1, Request.UserHostAddress);

        //        model.ParkZones = ParkZoneModel.LoadParkZones(model.Parking.ParkingID);

        //        return View("ParkZones", model);
        //    }

        //    return View(model);
        //}

        public ActionResult LiveReservations()
        {
            return View("ReservationsList", new ReservationListModel(HttpContext, ReservationListModel.ReservationListType.Global));
        }

        [HttpPost]
        public ActionResult DeleteLiveReservation(string id)
        {
            var ids = id.Split('-');
            var sessionId = ids[0];
            id = ids[1];
            ReservationsManager.RemoveLiveReservations(HttpContext.ApplicationInstance.Application, sessionId, Convert.ToInt32(id));
            return LiveReservations();
        }

        public ActionResult ProcessingReservations()
        {
            return View("ReservationsList", new ReservationListModel(HttpContext, ReservationListModel.ReservationListType.Processing));
        }

        [HttpPost]
        public ActionResult DeleteProcessingReservation(int id)
        {
            ReservationsManager.RemoveProcessingReservations(id);
            return ProcessingReservations();
        }
    }
}